import java.util.ArrayList;
import java.util.List;

public class ScopeManager {

	private class TreeNode {
		private ScopeInfo sinfo;
		private List<TreeNode> children;
		private TreeNode parent;
		private int level = 0;

		public TreeNode() {
			super();
			children = new ArrayList<TreeNode>();
			sinfo = null;
			parent = null;
		}

		public void setLevel(int level) {
			this.level = level;
		}

		public int getLevel() {
			return level;
		}

		public void addChild(TreeNode child) {
			children.add(child);
			child.parent = this;
		}

		public void setScopeInfo(ScopeInfo info) {
			this.sinfo = info;
		}
	}

	private static ScopeManager scopeMgr;
	private TreeNode rootNode;
	private TreeNode currentNode;
	private int level = 0;

	// Singleton instance and hence private constructor.
	private ScopeManager() {
		rootNode = new TreeNode();
		currentNode = rootNode;
	}

	public static ScopeManager getScopeManager() {
		if (scopeMgr == null) {
			scopeMgr = new ScopeManager();
		}
		return scopeMgr;
	}

	public ScopeInfo getCurrentScope() {
		return currentNode.sinfo;
	}

	public ScopeInfo createNewScope() {
		TreeNode newNode = new TreeNode();
		ScopeInfo newScope = new ScopeInfo();
		currentNode.addChild(newNode);
		newNode.setScopeInfo(newScope);
		newNode.setLevel(level);
		currentNode = newNode;

		++level;
		return newScope;
	}

	public ScopeInfo popLastScope() {
		--level;
		ScopeInfo temp = currentNode.sinfo;
		currentNode = currentNode.parent;
		return temp;
	}

	public int getCurrentScopeCount() {
		return currentNode.getLevel();
	}
	
	public int getBlockLevel(){
		return level;
	}
}
